package com.fbs.demo1111.aop.aspect;

import com.fbs.demo1111.aop.permission.MyPermission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Component
@Aspect
public class PermissionAdvice {

    private List<String> permissions = new ArrayList<>();

    public PermissionAdvice() {
        permissions.add("list");
        permissions.add("delete");
    }

    @Pointcut("@annotation(com.fbs.demo1111.aop.permission.MyPermission)")
    public void permis() {
    }

    @Around("permis()")
    public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        MyPermission annotation = method.getAnnotation(MyPermission.class);

        if (annotation != null) {
            String value = annotation.value();
            if (!permissions.contains(value)) {
                System.out.println("权限不足");
                return "权限不足";

            }
        }
        return joinPoint.proceed();

    }


}